package list;

public class HuanXingLianBiaoQieQuDian {
    public static void main(String[] args) {
        Node head = new Node(8);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(1);
        head.next.next.next.next = new Node(9);
        head.next.next.next.next = new Node(10);

        head.next.next.next.next.next = head.next;//形成环，从末尾节点应用前面的节点

        Node q = pointcut(head);
        System.out.println(q.value);

//        boolean b = hasCircle(head);
//        System.out.println("是否有环："+b);
    }

    private static Node pointcut(Node head) {
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next !=null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow==fast){
                slow = head;
                while (slow != fast){
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }

    static class Node{
        int value;
        Node prev;
        Node next;
        public Node(int value){
            this.value=value;
        }
    }
}
